package bookManage.Controller.servlet;

import bookManage.Model.entity.Book;
import bookManage.Model.service.ServiceImpl;
import bookManage.utils.DBUtil;
import bookManage.utils.JsonUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

/**
 * @BelongsProject: library-management-system-master
 * @BelongsPackage: bookManage.Controller
 * @Author: yuan wang
 * @CreateTime: 2023-02-02  21:46
 * @Description: 展示书籍详细信息的Servlet，由BookShow页面中每本书籍点击跳转而来，
 * 跳转进来后将展示该书籍的所有同名书籍的状态，也会有前面的更详细的介绍等等
 * @Version: 1.0
 */
@WebServlet(name = "BookContent", value = "/manager/BookShow/BookContent")
public class BookContent extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String jsonString = String.valueOf(JsonUtil.getJson(req));
        JSONObject jsonObject = JSON.parseObject(jsonString);

        ServiceImpl service = new ServiceImpl();

        //通过键值对获取前端传来的number值
        String number = jsonObject.getString("number");

        PrintWriter out = resp.getWriter();
        JSONObject object = new JSONObject();

        try {
            //创建 Book实体类--book 和 Book的List集合--bookContentList
            Book bookContent = service.queryBookContent(number);

            List<bookManage.Model.entity.BookContent> bookContentList = service.queryBookContentList(number);

            //如果book为空或者bookContentList中没有数据，说明查询失败，也就是没有这个相关记录
            if (bookContent == null || bookContentList == null) {
                object.put("code", "-1");
                object.put("message", "查询失败");
                out.println(object);
            } else {
                //如果book和bookContentList都有数据，则查询成功，先返回书籍总体的详细信息
                object.put("code", "200");
                object.put("bookName", bookContent.getBookName());
                object.put("writer", bookContent.getWriter());
                object.put("kind", bookContent.getKind());
                object.put("number", bookContent.getNumber());
                object.put("publish", bookContent.getPublish());
                object.put("bookIntroduction", bookContent.getBookIntroduction());
                object.put("writerIntroduction", bookContent.getWriterIntroduction());
                object.put("picture", bookContent.getPicture());
                object.put("countAll", bookContent.getCountAll());
                object.put("countExist", bookContent.getCountExist());
                object.put("countLend", bookContent.getCountLend());

                //再返回相同书籍中每一本的具体状况
                jsonString = JSONObject.toJSONString(bookContentList);
                object.put("bookContentList", jsonString);
                out.println(object);
            }
        } catch (SQLException e) {
            object.put("code", "-1");
            object.put("message", "查询失败");
            out.println(object);
        } finally {
            try {
                DBUtil.freeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGet(req, resp);
    }
}
